package org.example.controller;

import cn.jiyun.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.config.R;
import org.example.pojo.User;
import org.example.pojo.vo.LoginReqVo;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j

public class UserController {
    @Autowired
    private UserService userService;

//    登录
    @RequestMapping("/login")
    public R user(@RequestBody User user, HttpSession session) {
        LambdaQueryWrapper<User> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shopLambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User userDB = userService.login(user);
        System.out.println(userDB);
        if (userDB == null) {
            return R.error("手机号或密码错误");
        }
        session.setAttribute("user", userDB);
        return R.ok().put("user", userDB);
    }

//    注册新用户
    @RequestMapping("/register")
    public void register(@RequestBody User user) {

        user.setUsername("123");
        user.setAvatarUrl("https://hong-honor.oss-cn-beijing.aliyuncs.com/user/2024/11/25/bb313bf403564322b1f887e7d9afaa5a.jpg");
        userService.register(user);
    }
//    查询所有手机号

    @RequestMapping("/findAllPhone")
    public List<User> findAllPhone() {
        return userService.findAllPhone();
    }
//    发送验证码
@RequestMapping("/sendCode/{phone}")
public R sendCode(@PathVariable String phone, String code) throws Exception {
    return userService.sendCode(phone, code);
}
//忘记密码
@RequestMapping("/checkPhone")
public R checkPhone(@RequestBody User user) {
    LambdaQueryWrapper<User> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
    shopLambdaQueryWrapper.eq(User::getPhone, user.getPhone());
    User phoneDB = userService.checkPhone(user);
    if (phoneDB == null) {
        return R.error("手机号不存在");
    }
    return R.ok().put("phone", phoneDB);
}

    @RequestMapping("/forget")
    public void forget(@RequestBody User user) {
        userService.updatePassword(user);
    }
}
